import bean.UserDao;
import bean.UserService;
import cn.hutool.core.io.IoUtil;
import com.tyfspring.beans.config.BeanDefinition;
import com.tyfspring.beans.config.BeanReference;
import com.tyfspring.beans.config.PropertyValue;
import com.tyfspring.beans.config.PropertyValues;
import com.tyfspring.beans.factory.DefaultListableBeanFactory;
import com.tyfspring.beans.support.XmlBeanDefinitionReader;
import com.tyfspring.core.io.DefaultResourceLoader;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Description TODO
 * @Author shallow
 * @Date 2023/3/27 18:15
 */

public class test {

    DefaultListableBeanFactory beanFactory=new DefaultListableBeanFactory();

    private DefaultResourceLoader resourceLoader = new DefaultResourceLoader();

    @Test
    public void test_BeanFactory1(){
        //注册bean
        BeanDefinition beanDefinition = new BeanDefinition(UserService.class);
        beanFactory.registerBeanDefinition("userService", beanDefinition);
        //第一次获取bean
        UserService userService = (UserService) beanFactory.getBean("userService");
        userService.queryUserInfo();
        //第二次获取bean from Singleton
        UserService service = (UserService) beanFactory.getBean("userService");
    }

    @Test
    public void test_BeanFactory2(){
        //注册bean
        BeanDefinition beanDefinition = new BeanDefinition(UserService.class);
        beanFactory.registerBeanDefinition("userService", beanDefinition);
        UserService bean = (UserService) beanFactory.getBean("userService", "卢本伟");
        bean.queryUserInfo();

    }

    @Test
    public void test_applyPropertyValue(){
        beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));
// 3. UserService 设置属性[uId、userDao]
        PropertyValues propertyValues = new PropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("uid", "10001"));
        propertyValues.addPropertyValue(new PropertyValue("userDao",new BeanReference("userDao")));
// 4. UserService 注入 bean
                BeanDefinition beanDefinition = new BeanDefinition(UserService.class, propertyValues);
        beanFactory.registerBeanDefinition("userService", beanDefinition);
// 5. UserService 获取 bean
        UserService userService = (UserService) beanFactory.getBean("userService");
        userService.queryUserInfo();

    }


    @Test
    public void test6() throws IOException {
//        InputStream inputStream = resourceLoader.getResource("classpath:important.properties").getInputStream();
//        String content = IoUtil.readUtf8(inputStream);
//        System.out.println("content = " + content);
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions("classpath:spring.xml");
        UserService userService = beanFactory.getBean("userService", UserService.class);
        userService.queryUserInfo();

    }

    @Test
    public void test7(){



    }
}
